package com.example.testinternet;

import android.Manifest;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.testinternet.bean.Weather;
import com.example.testinternet.bean.Yesterday;
import com.example.testinternet.utils.ParseWeather;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import java.util.logging.LogRecord;

import pub.devrel.easypermissions.EasyPermissions;
class MyMessage{
    int flag;
    String dataObject;
}
public class MainActivity extends AppCompatActivity {
    String  jsonData =  "" ;
//    String ip="http://10.0.2.2:8080/weather.json";
    String ip="http://t.weather.sojson.com/api/weather/city/101080201";
    private TextView tvWeather;
    static String message1="";
    static int flag=0;
    MyMessage myMessage=new MyMessage();
//    Handler handler=new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            //收到消息后要做的处理
//            ;
//            tvWeather.setText(msg.obj.toString());
//        }
//    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //权限
        String[]perms={Manifest.permission.INTERNET};
        if (!EasyPermissions.hasPermissions(this,perms)){
            EasyPermissions.requestPermissions(this,"internet",1,perms);
        }
        tvWeather = (TextView) findViewById(R.id.tv_w);
        //访问internet，获取天气json

        requestWeatherJsonFromNet( );
        while(myMessage.flag==0){

        }
        tvWeather.setText(""+myMessage.dataObject);
        Log.i("MainActivity", "flag--"+flag);
        //Log.i("MainActivity", "message--"+message);





    }

    private void requestWeatherJsonFromNet( ) {
        //耗时操作，放线程


        new Thread(new Runnable() {
            @Override
            public void run() {
                flag=0;
                //1-封装ip
                try {
                    jsonData="";
                    URL url=new URL(ip);
                    //2-创建连接
                    HttpURLConnection con= (HttpURLConnection) url.openConnection();
                    //3-设置超时时间
                    con.setConnectTimeout(2000);
                    //4-获取服务器返回的输入流

                    if (con.getResponseCode()==200/*连接成功*/){
                        //Thread.sleep(1000);
                        InputStream in = con.getInputStream();
                        BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
                        String line = "";
                        while ((line = br.readLine()) != null) {
                            jsonData = jsonData+ line;
                        }
                        br.close();

                        Log.i("MainActivity", "run: "+ jsonData );
                        //解析天气数据
                        //创建工具类
                        if (!jsonData.equals("")) {
                            Gson gson=new Gson();


                            Weather wether = gson.fromJson(jsonData.trim(),Weather.class);
                            ArrayList<Yesterday> forecast=wether.getData().getForecast();
                            final String day0=forecast.get(0).getHigh();
                            final String day1=forecast.get(1).getHigh();
                            final String day2=forecast.get(2).getHigh();
                            //显示
                            Log.i("MainActivity", "day0--"+day0);
                            Log.i("MainActivity", "day1--"+day1);
                            Log.i("MainActivity", "day2--"+day2);
                            //显示在文本框
                            myMessage.flag=1;
                            message1=day0+"--"+day1+"--"+day2;
                            myMessage.dataObject=message1;
                            Log.i("MainActivity", "flag--"+flag);
//                            Message message=new Message();
//                            message.obj=message1;
//                            handler.sendMessage(message);

//                            runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    tvWeather.setText(day0+"--"+day1+"--"+day2);
//                                }
//                            });
                        }

                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }


            }
        }).start();

    }
}
